<template>
	<view>
		<!-- 顶部导航栏 -->
		<view class="bar" :style="[{ height: StatusBar + 'px' }]"></view>
		<view class="header" :style="[{ top: StatusBar + 'px' }]">
			<!-- 定位城市 -->
			<view class="addr" @tap="selectCity">
				<view class="iconfont icon-dizhi"></view>
				{{addressName}}
			</view>
			<!-- 搜索框 -->
			<!-- <view class="input-box">
				<input placeholder="请输入商品名称" placeholder-style="color:#c0c0c0;" @tap="toSearch()" />
				<view class="iconfont icon-xingtaiduICON_sousuo--"></view>
			</view> -->
			<view class="input-box">
				<image height="60upx" src="../../static/img/utils/logo1.png" mode="widthFix"></image>
			</view>
			<!-- 右侧图标按钮 -->
			<view class="icon-btn">
				<view class="iconfont icon-gouwuche" @tap="toShopcart"></view>
				<view class="iconfont icon-xiaoxi2" :class="{ isMsg: isMsg }" @tap="toMsg"></view>
			</view>
		</view>
		<!-- 轮播图 -->
		<view class="swiper" :style="[{ paddingTop: isMPheight + 'px' }]">
			<view class="swiper-box">
				<swiper circular="true" autoplay="true" @change="swiperChange">
					<swiper-item v-for="swiper in swiperList" :key="swiper.id">
						<image :src="swiper.img" @tap="toSwiper(swiper)" mode="widthFix"></image>
					</swiper-item>
				</swiper>
				<view class="indicator">
					<view class="dots" v-for="(swiper, index) in swiperList" :class="[currentSwiper >= index ? 'on' : '']" :key="index"></view>
				</view>
			</view>
		</view>
		<!-- 分类 -->
		<view class="cate-section">
			<view class="cate-item" @click="toCarList(1)">
				<image src="https://nc.jieqinwang.com/Public/images/mobile/icon-search.png"></image>
				<text>自选婚车</text>
			</view>
			<view class="cate-item" @click="toCarList(0)">
				<image src="https://nc.jieqinwang.com/Public/images/mobile/icon-taocan.png"></image>
				<text>精选车队</text>
			</view>
			<navigator url="../car/help" hover-class="navigator-hover" class="cate-item">
				<image src="https://nc.jieqinwang.com/Public/images/mobile/icon-car.png"></image>
				<text>帮我选车</text>
			</navigator>
			<view class="cate-item">
				<image src="https://nc.jieqinwang.com/Public/images/mobile/icon-car.png"></image>
				<text>发布车辆</text>
			</view>
			<navigator url="../news/news_list" hover-class="navigator-hover" class="cate-item">
				<image src="https://nc.jieqinwang.com/Public/images/mobile/icon-heart.png"></image>
				<text>婚车故事</text>
			</navigator>
		</view>

		<view class="cate2-section">
			<navigator url="../store/store_list" hover-class="navigator-hover" class="cate2-item">
				<view class="cate2-title">结婚商家</view>
				<view class="cate2-desc">4个字标题</view>
				<image src="../../static/img/utils/cate-1.png"></image>
			</navigator>
			<navigator url="../news/news_list" class="cate2-item">
				<view class="cate2-title">备婚研究院</view>
				<view class="cate2-desc">4个字标题</view>
				<image src="../../static/img/utils/cate-2.png"></image>
			</navigator>
			<view class="cate2-item">
				<view class="cate2-title">结婚工具</view>
				<view class="cate2-desc">4个字标题</view>
				<image src="../../static/img/utils/cate-3.png"></image>
			</view>
		</view>
		<!-- <view class="ad-1">
			<image src="/static/img/temp/ad1.jpg" mode="scaleToFill"></image>
		</view> -->

		<!-- 推荐1 -->
		<view class="recommend-section m-t">
			<view class="recommend-title">
				<view class="iconfont icon-xingzhuang"></view>
				{{addressName}}婚车推荐
				<view @click="toCarList(1)" class="recommend-title-more">
					查看更多
					<view class="iconfont icon-left"></view>
				</view>
			</view>
			<view class="recommend-list" v-if="carRecommend.length">
				<template v-for="(item, index) in carRecommend" :index="index">
					<navigator :url="'../car/cardetail?id=' + item.id + '&type=1'" hover-class="none" class="recommend-item">
						<image :src="item.imageArray !== null ? item.imageArray[0] : ''"></image>
						<view class="recommend-label">
							<text>{{ item.brandName }}</text>
							{{ item.modelName }}
						</view>
						<view class="recommend-price">
							￥{{ item.jieqinPrice }}
							<text class="recommend-old-price">市场价￥{{ item.marketPrice }}</text>
						</view>
					</navigator>
				</template>
			</view>
		</view>

		<!-- 推荐2 -->
		<view class="recommend-section m-t">
			<view class="recommend-title">
				<view class="iconfont icon-tuijian"></view>
				精选车队推荐
				<view @click="toCarList(0)" class="recommend-title-more">
					查看更多
					<view class="iconfont icon-left"></view>
				</view>
			</view>
			<view class="recommend2-list" v-if="carRecommendPackage.length">
				<template v-for="(item, index) in carRecommendPackage" :index="index">
					<navigator :url="'../car/cardetail?id=' + item.id + '&type=2'" hover-class="none" class="recommend2-item">
						<image :src="item.imageArray !== null ? item.imageArray[0] : ''"></image>
						<view class="recommend2-label">
							<text class="label-1">头车：</text>
							<text class="label-2">{{ item.mainBrandName }}</text>
							{{ item.mainModelName }}/{{ item.mainNum }}辆
						</view>
						<view class="recommend2-label">
							<text class="label-1">跟车：</text>
							<text class="label-2">{{ item.normalBrandName }}</text>
							{{ item.normalModelName }}/{{ item.normalNum }}辆
						</view>
						<view class="recommend2-price">
							￥{{ item.jieqinPrice }}
							<text class="recommend-old-price">市场价￥{{ item.marketPrice }}</text>
						</view>
					</navigator>
				</template>
			</view>
			<view @click="toCarList(0)" class="load_more">
				查看所有婚车套餐
				<view class="iconfont  icon-left"></view>
			</view>
		</view>
		<u-loading :visible="loading"></u-loading>
	</view>

</template>

<script>
	import amap from '../../utils/amap-wx.js';
	import {
		mapMutations,mapState
	} from 'vuex';
	export default {
		data() {
			return {
				loading: true,
				isMPheight: 0,
				key: 'a8691091a3ef40a126a578e746af1e43',
				amapPlugin: null,
				addressName: '西安',
				StatusBar: this.StatusBar, // 状态栏高度
				isMsg: true, //有消息时
				currentSwiper: 0, //第几个轮播图
				// 轮播图片
				swiperList: [{
						id: 1,
						src: 'url1',
						img: 'https://qnm.hunliji.com/o_1d1dilnb31ipo1i0k1upb16vj4r29.jpg?imageView2/1/w/686/h/276'
					},
					{
						id: 2,
						src: 'url2',
						img: 'https://qnm.hunliji.com/o_1d1diprgh1nms19mc1kkn40k1r2h9.jpg?imageView2/1/w/686/h/276'
					},
					{
						id: 3,
						src: 'url3',
						img: 'http://qnm.hunliji.com/o_1cldkvljg17o736catf14fukn09.jpg?imageView2/1/w/686/h/276'
					}
				],
				//推荐婚车
				carRecommend: [],
				carRecommendPackage: []
			};
		},
		onLoad: function(options) {
			this.isMPheight = uni.upx2px(100) + this.StatusBar;
			// #ifndef H5
			this.amapPlugin = new amap.AMapWX({
				key: this.key
			});
			this.getCity();
			// #endif
			Promise.all([this.getCarRecommend(), this.getCarRecommendPackage()]).then(r => {
				setTimeout(() => {
					this.loading = false;
					uni.hideLoading();
				}, 500)
			})
		},
		computed:{
			...mapState(['hasLogin'])
		},
		methods: {
			...mapMutations(['cityId', 'current']),
			toCarList(type) {
				this.current(type);
				uni.switchTab({
					url: '../car/carlist'
				});
			},
			/**
			 * 点击搜索框跳到search页
			 */
			toSearch() {
				uni.navigateTo({
					url: 'search'
				});
			},
			/**
			 * 获取定位
			 */
			getCity() {
				this.amapPlugin.getRegeo({
					success: (data) => {
						console.log(data[0])
						this.addressName = data[0].regeocodeData.addressComponent.city.replace(/市/g, '');;
						let adcode = data[0].regeocodeData.addressComponent.adcode;
						this.cityId(adcode);
					}
				});
			},
			/**
			 * 滑动轮播图
			 */
			swiperChange(e) {
				this.currentSwiper = e.detail.current;
			},
			/**
			 * 点击轮播图
			 */
			toSwiper(swiper) {
				uni.showToast({
					title: '点击了图片' + swiper.src,
					icon: 'none',
					duration: 2000
				});
			},
			/**
			 * 点击消息
			 */
			toMsg() {
				if (!this.hasLogin) {
					uni.showModal({
						content: "您还没有登录，是否现在登录",
						confirmText: "确定",
						cancelText: "取消",
						success: (res) => {
							if (res.confirm) {
								uni.navigateTo({
									url: '../login/login'
								});
							}
						}
					})
				} else {
					uni.navigateTo({
						url: 'chat'
					});
				}
			},
			/**
			 * 点击购物车
			 */
			toShopcart(){
				if (!this.hasLogin) {
					uni.showModal({
						content: "您还没有登录，是否现在登录",
						confirmText: "确定",
						cancelText: "取消",
						success: (res) => {
							if (res.confirm) {
								uni.navigateTo({
									url: '../login/login'
								});
							}
						}
					})
				} else {
					uni.navigateTo({
						url: '../shopcart/cart'
					});
				}
			},
			/**
			 * 点击选择城市
			 */
			selectCity() {
				// uni.navigateTo({
				// 	url: 'selectCity'
				// });
			},
			/**
			 * 获取婚车推荐列表
			 */
			getCarRecommend() {
				this.$api.CarRecommend(this.$store.state.cityId, '1').then(res => {
					this.carRecommend = res.data;
				});
			},
			/**
			 * 获取婚车推荐套餐列表
			 */
			getCarRecommendPackage() {
				this.$api.CarRecommendPackage(this.$store.state.cityId, '1').then(res => {
					this.carRecommendPackage = res.data;
				});
			},
		}
	};
</script>

<style lang="scss">
	page {
		background: #f5f5f5;
	}

	.m-t {
		margin-top: 16upx;
	}

	.clamp {
		overflow: hidden;
		-o-text-overflow: ellipsis;
		text-overflow: ellipsis;
		white-space: nowrap;
		display: block;
	}

	.bar {
		width: 100%;
		background-color: #fff;
		position: fixed;
		top: 0;
		z-index: 10;
	}

	.header {
		width: 100%;
		padding: 0 4%;
		height: 100upx;
		display: flex;
		align-items: center;
		position: fixed;
		z-index: 10;
		background-color: #fff;

		.addr {
			width: 120upx;
			height: 60upx;
			flex-shrink: 0;
			display: flex;
			align-items: center;
			font-size: 28upx;

			.iconfont {
				height: 60upx;
				margin-right: 5upx;
				display: flex;
				align-items: center;
				font-size: 42upx;
				color: var(--pink);
			}
		}

		.input-box {
			width: 100%;
			height: 60upx;
			// background-color: #f5f5f5;
			// border-radius: 30upx;
			position: relative;
			display: flex;
			align-items: center;
			justify-content: center;

			.iconfont {
				display: flex;
				align-items: center;
				position: absolute;
				top: 0;
				right: 0;
				width: 60upx;
				height: 60upx;
				font-size: 34upx;
				color: #c0c0c0;
			}

			input {
				padding-left: 28upx;
				height: 60upx;
				line-height: 60upx;
				font-size: 28upx;
				width: 100%;
				padding-right: 80upx;
			}

			image {
				width: 200upx !important;
			}
		}

		.icon-btn {
			width: 120upx;
			height: 60upx;
			flex-shrink: 0;
			display: flex;

			.iconfont {
				width: 60upx;
				height: 60upx;
				display: flex;
				justify-content: flex-end;
				align-items: center;
				font-size: 42upx;
			}

			.isMsg {
				position: relative;

				&:after {
					content: '';
					display: block;
					height: 10upx;
					width: 10upx;
					border-radius: 50%;
					background-color: rgb(240, 108, 122);
					position: absolute;
					right: 0upx;
					top: 10upx;
				}
			}
		}
	}

	.place {
		background-color: #ffffff;
		height: 100upx;
		/*  #ifdef  APP-PLUS  */
		margin-top: var(--status-bar-height);
		/*  #endif  */
	}

	.swiper {
		width: 100%;
		margin-top: 10upx;
		background-color: #fff;
		display: flex;
		justify-content: center;

		.swiper-box {
			width: 92%;
			height: 30.7vw;
			overflow: hidden;
			border-radius: 15upx;
			box-shadow: 0upx 8upx 25upx rgba(0, 0, 0, 0.2);
			/* //兼容ios，微信小程序 */
			position: relative;
			z-index: 1;

			swiper {
				width: 100%;
				height: 30.7vw;

				image {
					width: 100%;
				}
			}

			.indicator {
				position: absolute;
				bottom: 20upx;
				left: 20upx;
				background-color: rgba(255, 255, 255, 0.4);
				width: 150upx;
				height: 5upx;
				border-radius: 3upx;
				overflow: hidden;
				display: flex;

				.dots {
					width: 100%;

					&.on {
						background-color: rgba(255, 255, 255, 1);
					}
				}
			}
		}
	}

	/* 分类 */
	.cate-section {
		display: flex;
		justify-content: space-around;
		align-items: center;
		flex-wrap: wrap;
		padding: 30upx 22upx;
		background: #fff;

		.cate-item {
			display: flex;
			flex-direction: column;
			align-items: center;
			font-size: $font-sm + 2upx;
			color: $font-color-dark;
		}

		/* 原图标颜色太深,不想改图了,所以加了透明度 */
		image {
			width: 88upx;
			height: 88upx;
			margin-bottom: 14upx;
			border-radius: 50%;
			opacity: 0.7;
			box-shadow: 4upx 4upx 20upx rgba(250, 67, 106, 0.3);
		}
	}

	/* 分类2 */
	.cate2-section {
		display: flex;
		background-color: #fff;
		padding-left: 20upx;
		padding-bottom: 20upx;

		.cate2-item {
			flex: 0 0 calc(670upx / 3);
			padding: 30upx 26upx;
			border-radius: 30upx;
			box-shadow: 0 4upx 24upx 0 rgba(0, 0, 0, 0.08);
			margin-right: 20upx;
			line-height: 1;
			position: relative;

			.cate2-title {
				font-size: 32upx;
				color: #000;
			}

			.cate2-desc {
				margin-top: 20upx;
				font-size: 24upx;
				color: #666;
			}

			image {
				position: absolute;
				bottom: 16upx;
				right: 24upx;
				width: 44upx;
				height: 44upx;
			}
		}
	}

	.ad-1 {
		width: 100%;
		height: 210upx;
		padding: 10upx 0;
		background: #fff;

		image {
			width: 100%;
			height: 100%;
		}
	}

	/* 推荐 */
	.recommend-section {
		background: #fff;

		.recommend-title {
			height: 96upx;
			display: flex;
			align-items: center;
			font-size: 32upx;
			padding: 0 20upx;
			color: #333;

			.iconfont {
				font-size: 32upx;
				color: var(--pink);
				margin: 0 10upx 0 0;
				font-weight: bold;
			}

			.recommend-title-more {
				font-size: 28upx;
				float: right;
				color: #999;
				display: flex;
				align-items: center;
				justify-content: flex-end;
				flex: auto;

				.iconfont {
					font-size: 28upx;
					color: #999;
					margin: 0 0 0 10upx;
				}
			}
		}

		.recommend-list {
			padding-left: 20upx;
			padding-top: 20upx;
			display: flex;
			flex-flow: wrap;

			.recommend-item {
				flex: 0 0 345upx;
				margin-right: 20upx;
				margin-bottom: 20upx;
				overflow: hidden;

				image {
					height: 200upx;
					width: 345upx;
					margin-bottom: 10upx;
				}

				.recommend-label {
					display: flex;
					height: 40upx;
					font-size: 28upx;
					color: #000;
					font-weight: 600;

					text {
						font-size: 24upx;
						color: var(--pink);
						padding: 0 10upx;
						border: 2upx solid var(--pink);
						border-radius: 4upx;
						margin-right: 10upx;
						font-weight: 400;
					}
				}

				.recommend-price {
					display: flex;
					height: 36upx;
					align-items: center;
					color: var(--red);
					font-size: 24upx;

					.recommend-old-price {
						flex: auto;
						display: flex;
						justify-content: flex-end;
						color: #999;
						text-decoration: line-through;
					}
				}
			}
		}

		.recommend2-list {
			.recommend2-item {
				padding: 30upx 20upx;
				border-bottom: 2upx solid #f5f5f5;

				image {
					height: 400upx;
					width: 100%;
				}

				.recommend2-label {
					display: flex;
					height: 60upx;
					font-size: 32upx;
					color: #000;
					align-items: center;

					.label-1 {
						color: #999;
						height: 60upx;
						align-items: center;
						display: flex;
						line-height: 1;
					}

					.label-2 {
						font-size: 24upx;
						color: var(--pink);
						padding: 0 10upx;
						border: 2upx solid var(--pink);
						border-radius: 4upx;
						margin-right: 10upx;
					}
				}

				.recommend2-price {
					display: flex;
					height: 36upx;
					align-items: center;
					color: var(--red);
					font-size: 30upx;

					.recommend-old-price {
						font-size: 24upx;
						flex: auto;
						display: flex;
						justify-content: flex-end;
						color: #999;
						text-decoration: line-through;
					}
				}
			}
		}

		.load_more {
			height: 90upx;
			background-color: #fff;
			display: flex;
			align-items: center;
			justify-content: center;
			color: #999;
			font-size: 32upx;
			margin-bottom: 90upx;

			.iconfont {
				font-size: 32upx;
				color: #999;
				margin-left: 10upx;
			}
		}
	}
</style>
